package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.R;
import com.itheima.reggie.common.UserHolder;
import com.itheima.reggie.entify.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 查询购物车
     * @return
     */
    @GetMapping("/list")
    public R list(){
        LambdaQueryWrapper<ShoppingCart> qw = new LambdaQueryWrapper<>();
        qw.eq(ShoppingCart::getUserId,UserHolder.getUserId());
        qw.orderByDesc(ShoppingCart::getCreateTime);
        List<ShoppingCart> list = shoppingCartService.list(qw);
        return  R.success(list);
    }

    /**
     * 添加至购物车
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public R  save(@RequestBody ShoppingCart shoppingCart){
        //获取用户id，指定当前是哪个用户的购物车数据
       Long userId = UserHolder.getUserId();
       shoppingCart.setUserId(userId);

        Long dishId = shoppingCart.getDishId();

        LambdaQueryWrapper<ShoppingCart> qw = new LambdaQueryWrapper<>();
        qw.eq(ShoppingCart::getUserId,userId);
        //判断添加到购物车的是菜品还是套餐
        if(dishId!=null){
            //添加的是菜品
            qw.eq(ShoppingCart::getDishId,dishId);
        }else{
            //添加的是套餐
            qw.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        //添加成功后，获取添加菜品或者套餐对象
        ShoppingCart one = shoppingCartService.getOne(qw);
        //查询当前加入购物车的商品是否在购物车
        if(one!=null){
            //one!=null说明购物车已经存在该商品，number数量+1
            Integer number = one.getNumber();
            one.setNumber(number+1);
            //更新购物车该商品数量
            shoppingCartService.updateById(one);
        }else{
            //one==null说明购物车中没有该商品，添加该商品，默认数量就是加1
            shoppingCart.setNumber(1);
            shoppingCart.setUserId(userId);
            shoppingCartService.save(shoppingCart);
        }
        return R.success("添加购物车成功");
    }

    /**
     * 取消购物车操作
     * @param shoppingCart
     * @return
     */
    @PostMapping("/sub")
    public R sub(@RequestBody ShoppingCart shoppingCart){
        //获取用户id，指定当前是哪个用户的购物车数据
        Long userId = UserHolder.getUserId();
        LambdaQueryWrapper<ShoppingCart> qw = new LambdaQueryWrapper<>();
        qw.eq(ShoppingCart::getUserId,userId);
        //获取菜品的id
        Long dishId = shoppingCart.getDishId();
        //判断取消的是套餐还是菜品
        if(dishId!=null){
            //dishId!=null结果是有菜品，说明是菜品
            qw.eq(ShoppingCart::getDishId,dishId);
        }else{
            //是套餐
            qw.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        ShoppingCart one = shoppingCartService.getOne(qw);
        //执行number-1操作
        Integer number = one.getNumber();
        number--;
        //再次赋值number
        one.setNumber(number);

        if(number>0){
            //说明购物车里面还有菜品或者套餐，执行更新操作
            shoppingCartService.updateById(one);
        }else{
            //执行删除操作
            shoppingCartService.remove(qw);
        }

        return R.success(one);
    }

    /**
     * 清空购物车
     * @return
     */
    @DeleteMapping("/clean")
    public R clean(){
        Long userId = UserHolder.getUserId();
        LambdaQueryWrapper<ShoppingCart> qw = new LambdaQueryWrapper<>();
        qw.eq(ShoppingCart::getUserId,userId);
        shoppingCartService.remove(qw);
        return R.success("清空购物车成功");
    }
}
